package com.cgy.utils.base.limiter;

import com.cgy.utils.base.limiter.provider.ILimiterProvider;
import com.google.common.util.concurrent.RateLimiter;

import java.util.concurrent.TimeUnit;

/**
 * @author CH
 */
public class TokenLimitProvider implements ILimiterProvider {

    private RateLimiter rateLimiter = null;

    @Override
    public ILimiterProvider newLimiter(int size) {
        rateLimiter = RateLimiter.create(size);
        return this;
    }

    @Override
    public boolean tryAcquire() {
        return null != rateLimiter ? rateLimiter.tryAcquire() : false;
    }

    @Override
    public boolean tryAcquire(long time) {
        return null != rateLimiter ? rateLimiter.tryAcquire(time, TimeUnit.SECONDS) : false;
    }

    @Override
    public boolean tryAcquire(long time, TimeUnit timeUnit) {
        return null != rateLimiter ? rateLimiter.tryAcquire(time, timeUnit) : false;
    }

    @Override
    public boolean tryGet() {
        return false;
    }
}
